﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;
using WHC.Security.Entity;

namespace WHC.Hotel.UI.Report
{
    public partial class KFMoneyDetailReport : BaseForm
    {
        public KFMoneyDetailReport()
        {
            InitializeComponent();
            InitDictItem();
        }

        private void InitDictItem()
        {
            this.txtPayType.Items.Clear();
            this.txtPayType.Items.AddRange(DictItemUtil.GetPayType());
            if (this.txtPayType.Items.Count > 0)
            {
                this.txtPayType.SelectedIndex = 0;
            }

            this.txtCreator.Items.Clear();
            List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            foreach (UserInfo info in userList)
            {
                this.txtCreator.Items.Add(info.FullName);
            }
            this.txtCreator.Items.Insert(0, "所有收银人");
            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private void KFMoneyDetailReport_Load(object sender, EventArgs e)
        {
            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;

            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

        }

        private void BinData()
        {
            this.lblCreator.Text = this.txtCreator.Text;

            #region 添加别名解析
            this.winGridView1.AddColumnAlias("RoomNo", "房间号");
            this.winGridView1.AddColumnAlias("Customer_ID", "宾客编号");
            this.winGridView1.AddColumnAlias("CustomerName", "宾客姓名");
            this.winGridView1.AddColumnAlias("PayDate", "收款时间");
            this.winGridView1.AddColumnAlias("PayType", "收款方式");
            this.winGridView1.AddColumnAlias("Cash", "现金");
            this.winGridView1.AddColumnAlias("Card", "储值卡");
            this.winGridView1.AddColumnAlias("DaijinTicket", "代金劵");
            this.winGridView1.AddColumnAlias("NationalCredit", "国内信用卡");
            this.winGridView1.AddColumnAlias("InterCredit", "国际信用卡");
            this.winGridView1.AddColumnAlias("CashierType", "收银类别");
            this.winGridView1.AddColumnAlias("Creator", "操作员");
            this.winGridView1.AddColumnAlias("Note", "备注信息");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("PayDate", this.dtStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
                .AddCondition("PayDate", this.dtEnd.Value.ToString(), SqlOperator.LessThanOrEqual)
                .AddCondition("PayType", this.txtPayType.Text, SqlOperator.Like)
                .AddCondition("RoomNo", this.txtRoomNo.Text, SqlOperator.Like);
            if (string.IsNullOrEmpty(this.txtCreator.Text) || this.txtCreator.Text == "所有收银人")
            {
                this.lblCreator.Text = "所有收银人";
            }
            else
            {
                condition.AddCondition("Creator", this.txtCreator.Text, SqlOperator.Like);
            }
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            DataTable dt = BLLFactory<MoneyDetail>.Instance.GetMoneyDetailReport(filter);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.NewRow();
                dr["RoomNo"] = "合计：";
                decimal cash = 0M;
                decimal card = 0M;
                decimal daijin = 0M;
                decimal nationalCredit = 0M;
                decimal interCredit = 0M;
                foreach (DataRow row in dt.Rows)
                {
                    cash += Convert.ToDecimal(row["Cash"]);
                    card += Convert.ToDecimal(row["Card"]);
                    daijin += Convert.ToDecimal(row["DaijinTicket"]);
                    nationalCredit += Convert.ToDecimal(row["NationalCredit"]);
                    interCredit += Convert.ToDecimal(row["InterCredit"]);
                }
                dr["Cash"] = cash;
                dr["Card"] = card;
                dr["DaijinTicket"] = daijin;
                dr["NationalCredit"] = nationalCredit;
                dr["InterCredit"] = interCredit;

                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dt.NewRow());
                dt.Rows.Add(dr);
            }
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "收银明细查询报表";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BinData();
        }

        private void txtPayType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BinData();
            }
        }

        private void txtCreator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BinData();
            }
        }

        private void menu_Refresh_Click(object sender, EventArgs e)
        {
            BinData();
        }
    }
}
